﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Linq;
using Hikari.Common;
using Hikari.Areas.Administrator.Common;
using Hikari.Areas.Administrator.Models;
using System.Collections;

namespace Hikari.Areas.Administrator.Controllers
{
    public class CategoriesController : Controller
    {
        CategoriesDB db = new CategoriesDB();
        public ActionResult Index()
        {
            List<Category> list = new List<Category>();
            list = db.List();
            return View(list);
        }
        public ActionResult Add()
        {
            List<SelectListItem> itemsType = new List<SelectListItem>();
            itemsType.Add(new SelectListItem
            {
                Text = "Tin tức",
                Value = "1"
            });
            itemsType.Add(new SelectListItem
            {
                Text = "Sản phẩm",
                Value = "2"
            });
            ViewData["itemsType"] = itemsType;
            SapphireDataContext sa = new SapphireDataContext();
            List<SelectListItem> iCate = new List<SelectListItem>();            
            var item = (from a in sa.Categories orderby a.Ordinal ascending where a.ParentID==0 select a).ToList();
            iCate.Add(new SelectListItem { Text = "Danh mục cha", Value = "0" });

            foreach (var it in item)
            {
                iCate.Add(new SelectListItem { Text = " - " + it.Name, Value = it.ID.ToString() });
            }           
            ViewData["Cate"] = iCate;
            return View();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult Add(iCategory item)
        {
            List<SelectListItem> itemsType = new List<SelectListItem>();
            itemsType.Add(new SelectListItem
            {
                Text = "Tin tức",
                Value = "1"
            });
            itemsType.Add(new SelectListItem
            {
                Text = "Sản phẩm",
                Value = "2"
            });
            ViewData["itemsType"] = itemsType;
            SapphireDataContext sa = new SapphireDataContext();
            List<SelectListItem> iCate = new List<SelectListItem>();
            var ite = (from a in sa.Categories orderby a.Ordinal ascending where a.ParentID == 0 select a).ToList();
            iCate.Add(new SelectListItem { Text = "Danh mục cha", Value = "0" });

            foreach (var it in ite)
            {
                iCate.Add(new SelectListItem { Text = " - " + it.Name, Value = it.ID.ToString() });
            }
            ViewData["Cate"] = iCate;
            try
            {
                if (ValidateCategory(item))
                {
                    CategoriesDB CateDB = new CategoriesDB();
                    CateDB.Creates(item);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            SapphireDataContext sa = new SapphireDataContext();
            iCategory list = new iCategory();
            list = db.Get(id);
            List<SelectListItem> itemsType = new List<SelectListItem>();
            itemsType.Add(new SelectListItem
            {
                Text = "Tin tức",
                Value = "1"
            });
            if (list.Type == 2)
            {
                itemsType.Add(new SelectListItem
                {
                    Text = "Sản phẩm",
                    Value = "2",
                    Selected = true
                });
            }
            else
            {
                itemsType.Add(new SelectListItem
                {
                    Text = "Sản phẩm",
                    Value = "2"
                });
            }         
            ViewData["itemsType"] = itemsType;
            List<SelectListItem> iCate = new List<SelectListItem>();
            var item = (from a in sa.Categories orderby a.Ordinal ascending where a.ParentID == 0 select a).ToList();
            iCate.Add(new SelectListItem { Text = "Danh mục cha", Value = "0" });

            foreach (var it in item)
            {
                if (list.ParentID == it.ID)
                {
                    iCate.Add(new SelectListItem { Text = " - " + it.Name, Value = it.ID.ToString(), Selected = true });
                }
                else
                {
                    iCate.Add(new SelectListItem { Text = " - " + it.Name, Value = it.ID.ToString() });
                }
            }
            ViewData["Cate"] = iCate;
            return View(list);
        }
        //------------------------Validate---------------------------//
        private bool ValidateCategory(iCategory item)
        {
            if (String.IsNullOrEmpty(item.Name))
            {
                ModelState.AddModelError("Name", " ");
            }
            if (String.IsNullOrEmpty(item.Ordinal.ToString()))
            {
                ModelState.AddModelError("Ordinal", " ");
            }
            if (String.IsNullOrEmpty(item.Prefix))
            {
                ModelState.AddModelError("Prefix", " ");
            }
            if (String.IsNullOrEmpty(item.ParentID.ToString()))
            {
                ModelState.AddModelError("ParentID", " ");
            }
            if (String.IsNullOrEmpty(item.PriorityShow.ToString()))
            {
                ModelState.AddModelError("PriorityShow", " ");
            }
            return ModelState.IsValid;
        }
    }
}
